/**
* @project admin-go-api
* @name sysAdmin
* @descriptions 用户数据层
* @author 寒锋
* @date 2023-12-14 17:03:39
 */

package dao

import (
	"admin-go-api/api/entity"
	"admin-go-api/common/util"
	. "admin-go-api/pkg/db"
	"time"
)

// SysAdminDetailByLogin 根据登录信息获取管理员详情
func SysAdminDetailByLogin(dto entity.LoginDto) (sysAdmin entity.SysAdmin) {
	username := dto.Username
	Db.Where("username = ?", username).First(&sysAdmin)
	return sysAdmin
}

// GetSysAdminByDept 根据部门id获取管理员
func GetSysAdminByDept(deptId uint) (sysAdmin entity.SysAdmin) {
	Db.Where("dept_id = ?", deptId).First(&sysAdmin)
	return sysAdmin
}

// GetSysAdminByName 根据用户名获取管理员信息
func GetSysAdminByName(userName string, adminId int) (sysAdmin entity.SysAdmin) {
	model := Db.Where("username = ?", userName)
	if adminId != 0 {
		model = model.Where("admin_id <> ?", adminId)
	}
	model.First(&sysAdmin)
	return sysAdmin
}

// GetSysAdminById 根据id 获取管理员信息
func GetSysAdminById(adminId int) (sysAdmin entity.SysAdmin) {
	Db.First(&sysAdmin, adminId)
	return sysAdmin
}

// CreateSysAdmin 添加管理员
func CreateSysAdmin(dto entity.StoreSysAdminDto) error {
	data := entity.SysAdmin{
		PostId:     dto.PostId,
		DeptId:     dto.DeptId,
		Username:   dto.Username,
		Nickname:   dto.Nickname,
		Password:   util.EncryptionMd5(dto.Password),
		Phone:      dto.Phone,
		Email:      dto.Email,
		Note:       dto.Note,
		Status:     dto.Status,
		CreateTime: util.HTime{Time: time.Now()},
	}
	// 开始事务
	tx := Db.Begin()
	if err := tx.Error; err != nil {
		return err
	}
	if err := tx.Table("sys_admin").Create(&data).Error; err != nil {
		tx.Rollback()
		return err
	}
	roleData := entity.SysAdminRole{
		AdminId: data.AdminId,
		RoleId:  dto.RoleId,
	}
	if err := tx.Table("sys_admin_role").Create(&roleData).Error; err != nil {
		tx.Rollback()
		return err
	}
	return tx.Commit().Error
}

// GetSysAdminInfoById 根据id获取管理员信息
func GetSysAdminInfoById(adminId int) (sysAdmin entity.SysAdminInfo) {
	Db.Table("sys_admin").
		Select("sys_admin.*,sys_admin_role.role_id").
		Joins("left join sys_admin_role on sys_admin.admin_id = sys_admin_role.admin_id").
		Joins("left join sys_role on sys_admin_role.role_id = sys_role.role_id").
		First(&sysAdmin, adminId)
	return sysAdmin
}

// UpdateSysAdmin 修改管理员
func UpdateSysAdmin(dto entity.UpdateSysAdminDto) error {
	data := entity.SysAdmin{
		PostId: dto.PostId,
		DeptId: dto.DeptId,
		Status: dto.Status,
	}
	if dto.Username != "" {
		data.Username = dto.Username
	}
	if dto.Nickname != "" {
		data.Nickname = dto.Nickname
	}
	if dto.Phone != "" {
		data.Phone = dto.Phone
	}
	if dto.Email != "" {
		data.Email = dto.Email
	}
	if dto.Note != "" {
		data.Note = dto.Note
	}
	// 开启事务
	tx := Db.Begin()
	if err := tx.Error; err != nil {
		return err
	}
	// 更新管理员信息
	if err := Db.Table("sys_admin").Where("admin_id = ?", dto.AdminId).Updates(&data).Error; err != nil {
		tx.Rollback()
		return err
	}
	// 删除之前的管理员与角色关系
	if err := Db.Table("sys_admin_role").Where("admin_id = ?", dto.AdminId).Delete(&entity.SysAdminRole{}).Error; err != nil {
		tx.Rollback()
		return err
	}
	// 创建管理员与角色关系
	roleData := entity.SysAdminRole{
		AdminId: dto.AdminId,
		RoleId:  dto.RoleId,
	}
	if err := tx.Table("sys_admin_role").Create(&roleData).Error; err != nil {
		tx.Rollback()
		return err
	}
	return tx.Commit().Error
}

// DeleteSysAdmin 删除管理员
func DeleteSysAdmin(adminId int) error {
	tx := Db.Begin()
	if err := tx.Error; err != nil {
		return err
	}
	if err := tx.Table("sys_admin_role").Where("admin_id = ?", adminId).Delete(&entity.SysAdminRole{}).Error; err != nil {
		tx.Rollback()
		return err
	}
	if err := tx.Table("sys_admin").Delete(&entity.SysAdmin{}, adminId).Error; err != nil {
		tx.Rollback()
		return err
	}
	return tx.Commit().Error
}

// UpdateSysAdminState 修改管理员状态
func UpdateSysAdminState(dto entity.UpdateSysAdminStateDto) bool {
	data := entity.SysAdmin{
		Status: dto.Status,
	}
	result := Db.Where("admin_id = ?", dto.AdminId).Updates(&data)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}

// ResetSysAdminPassword 重置密码
func ResetSysAdminPassword(dto entity.ResetSysAdminPasswordDto) bool {
	data := entity.SysAdmin{
		Password: util.EncryptionMd5(dto.Password),
	}
	result := Db.Where("admin_id = ?", dto.AdminId).Updates(&data)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}

// GetSysAdminList 管理员列表
func GetSysAdminList(PageNum, PageSize int, Username, Nickname string, Status int, BeginTime, EndTime string) (sysAdmin []entity.SysAdminVo, count int64) {
	model := Db.Table("sys_admin").
		Select("sys_admin.*,sys_post.post_name,sys_role.role_name,sys_dept.dept_name").
		Joins("left join sys_post on sys_admin.post_id = sys_post.post_id").
		Joins("left join sys_admin_role on sys_admin.admin_id =sys_admin_role.admin_id").
		Joins("left join sys_role on sys_role.role_id = sys_admin_role.role_id").
		Joins("left join sys_dept on sys_admin.dept_id = sys_dept.dept_id")
	if Username != "" {
		model = model.Where("sys_admin.username = ?", Username)
	}
	if Nickname != "" {
		model = model.Where("sys_admin.nickname = ?", Nickname)
	}
	if Status != 0 {
		model = model.Where("sys_admin.status = ?", Status)
	}
	if BeginTime != "" && EndTime != "" {
		model = model.Where("sys_admin.create_time between ? and ?", BeginTime, EndTime)
	}
	model.Count(&count)
	model.Limit(PageSize).Offset((PageNum - 1) * PageSize).Order("sys_admin.create_time desc").Find(&sysAdmin)
	return sysAdmin, count
}

// UpdatePersonal 修改个人信息
func UpdatePersonal(dto entity.UpdatePersonalDto) bool {
	var data entity.SysAdmin
	if dto.Username != "" {
		data.Username = dto.Username
	}
	if dto.Nickname != "" {
		data.Nickname = dto.Nickname
	}
	if dto.ProfilePhoto != "" {
		data.ProfilePhoto = dto.ProfilePhoto
	}
	if dto.Phone != "" {
		data.Phone = dto.Phone
	}
	if dto.Email != "" {
		data.Email = dto.Email
	}
	if dto.Note != "" {
		data.Note = dto.Note
	}
	result := Db.Table("sys_admin").Where("admin_id = ?", dto.AdminId).Updates(&data)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}

// UpdatePersonalPassword 修改个人密码
func UpdatePersonalPassword(dto entity.UpdatePersonalPasswordDto) bool {
	data := entity.SysAdmin{
		Password: util.EncryptionMd5(dto.NewPassword),
	}
	result := Db.Table("sys_admin").Where("admin_id = ?", dto.AdminId).Updates(&data)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}
